package com.yueyang.sojourn.server.service.msg;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 自制自UC
 * @author andy.yang
 * @version v1
 * @summary 短信服务
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2019/3/29
 */
@Slf4j
@Service
public class SmsService {


    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    @Value("${aliyun.sms.access-key-id}")
    String accessKeyId;

    @Value("${aliyun.sms.access-key-secret}")
    String accessKeySecret;

    @Value("${aliyun.sms.sign-name}")
    String SignName;

    //创建订单通知短信模版
    @Value("${aliyun.sms.order-created-template-code}")
    String  orderCreatedTemplateCode;
    //订单即将过期通知短信模版
    @Value("${aliyun.sms.order-will-expire-template-code}")
    String  orderWillExpireTemplateCode;
    //订单已经过期通知短信模版
    @Value("${aliyun.sms.order-expired-template-code}")
    String  orderExpiredTemplateCode;
    //订单核销通知短信模版
    @Value("${aliyun.sms.order-dealed-template-code}")
    String  orderDealedTemplateCode;
    //订单成功退款通知短信模版
    @Value("${aliyun.sms.order-refunded-template-code}")
    String  orderRefundedTemplateCode;
    //订单核销身份验证短信模版
    @Value("${aliyun.sms.order-deal-captcha-template-code}")
    String  orderDealCaptchaTemplateCode;
    //订单退款身份验证短信模版
    @Value("${aliyun.sms.order-refund-captcha-template-code}")
    String  orderRefundCaptchaTemplateCode;

    /**
     *
     * @param phoneNumber
     * @param datas 数据
     * @param templateCode 模版code
     */
    public void sendMsg(String phoneNumber, Map<String,String> datas,String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", templateCode);
        JSONObject templateParam = new JSONObject();
        datas.keySet().stream().forEach((key)->{
            templateParam.put(key, datas.get(key));
        });
        request.putQueryParameter("TemplateParam", templateParam.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("========================短信结果========================================");
            log.info(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
        } catch (ClientException e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
        }
    }

    public String getOrderCreatedTemplateCode() {
        return orderCreatedTemplateCode;
    }

    public String getOrderWillExpireTemplateCode() {
        return orderWillExpireTemplateCode;
    }

    public String getOrderExpiredTemplateCode() {
        return orderExpiredTemplateCode;
    }

    public String getOrderDealedTemplateCode() {
        return orderDealedTemplateCode;
    }

    public String getOrderRefundedTemplateCode() {
        return orderRefundedTemplateCode;
    }

    public String getOrderDealCaptchaTemplateCode() {
        return orderDealCaptchaTemplateCode;
    }

    public String getOrderRefundCaptchaTemplateCode() {
        return orderRefundCaptchaTemplateCode;
    }
}
